// 02local/04foodOrder/foodOrder.js
var co = require('../../resource/js/common.js')
var app = getApp()
var _this

Page({
   data: {
      order: '',
      foodNum: 0,
      totalPrice: 0,
      // 菜品ID
      menuID: [],
      //上一页传递参数
      num: '',
      Table_ID: '',
      value: '',
      ifIndex: ''
   },

   onLoad: function (options) {
      _this = this
      var obj = JSON.parse(options.order)
      var ifIndex = options.id
      console.log("【输出】===", obj)
      var m = this.data.menuID
      for (var key in obj.order) {
         var a = obj.order[key]
         var o = {
            Product_ID: a.Products_ID,
            Sku_ID: a.sku_id,
            QTY: a.num
         }
         m.push(o)
      }
      this.setData({
         menuID: m,
         num: obj.Num,
         Table_ID: obj.Table_ID,
         value: obj.Value,
         ifIndex: ifIndex
      })
      console.log("【输出数组】===", m)
      this.countNumAndPrice(obj)
   },
   // 计算数量及价格
   countNumAndPrice: function (obj) {
      var num = 0
      var price = 0
      for (var i = 0; i < obj.order.length; i++) {
         num += obj.order[i].num * 1
         price += (obj.order[i].price * obj.order[i].num)
      }
      this.setData({
         order: obj,
         foodNum: num,
         totalPrice: price
      })
   },
   //加菜
   addFood: function () {
      wx.navigateBack({})
   },
   //提交菜单 
   submitOrder: function (e) {
      if (this.data.ifIndex == 0) {
         var address = app.Data.shopInfo
         var AP, AA, AC, AD, AM, AN, BI, CC, CI, IC, IG, IM, IUD, OI, OII, ON, OS, OSS, OT, OTT, TI, GN, SF
         // 判断订单为外卖还是本店
         if (app.Data.takeOut == 0) {
            AP = address.Biz_Province
            AA = address.Biz_Area
            AC = address.Biz_City
            AD = address.Biz_Address
            AM = address.Biz_Phone
            AN = address.Biz_Contact
            BI = 75      //address.Biz_ID
            OT = 'food'
            CC = 0
            CI = 0
            IC = 0
            IG = 0
            IM = 0
            IUD = 0
            OI = 0
            OII = 0
            ON = 0
            OS = "{'Express':'申通','Price':0}"
            OSS = 1
            OTT = 0
            TI = parseInt(this.data.Table_ID)
            GN = parseInt(this.data.num)
            SF = parseFloat(address.Seat_Fee)
         } else {

         }
         var param = { // 创建订单提交字段
            Address_Province: AP,                        //省份编号
            Address_Area: AA,                            //市区编号
            Address_City: AC,                            //城市编号
            Address_Detailed: AD,                        //详细地址
            Address_Mobile: AM,                          //收货人手机
            Address_Name: AN,                            //收获人姓名
            Biz_ID: BI,                                  //商家ID
            Coupon_Cash: CC,                             //优惠券抵现金
            Coupon_ID: CI,                               //使用优惠券ID
            Integral_Consumption: IC,                    //积分使用条件
            Integral_Get: IG,                            //商品积分
            Integral_Money: IM,                          //积分条件税换
            Is_User_Distribute: IUD,                     //买家是否分销商
            Order_InvoiceInfo: OI,                       //发票信息
            Order_IsVirtual: OII,                        //是否虚拟订单
            Order_NeedInvoice: ON,                       //是否需要发票
            Order_Shipping: OS,                          //快递
            Order_Status: OSS,                           //订单状态
            Order_TotalAmount: OTT,                      //订单原总价
            Order_Type: OT,                              //订单类型
            Table_ID: TI,                                //餐桌ID
            Seat_Fee: SF,                                //餐位费
            Guest_Num: GN,                               //用餐人数
            Order_Remark: e.detail.value.remark,         //订单备注
            Order_CartList: this.data.menuID,            //产品数量
            Order_TotalPrice: this.data.totalPrice,      //订单现总价
            Owner_ID: app.Data.userInfo.Owner_Id,        //推荐人ID
            User_ID: app.Data.userInfo.User_ID,          //用户ID
         }
         // console.log("【输出param】===", address)
         co.getShareApi('order/Omg/createFood', param, function (res) {
            // console.log('【获取返回信息】===', res.data, _this.data.value)
            if (res.code == 0) {
               if (_this.data.value == '预约') {
                  app.Data.Order_ID = res.data.Order_ID
                  wx.navigateBack({
                     delta: 2
                  })
               } else if (_this.data.value == '点餐') {
                  _this.getTableInfo(BI, TI)
               } else {
                  _this.getTableInfo(BI, TI)
               }
            }
         })
      } else {
         var param = {
            Order_ID: app.Data.Order_ID,                //订单ID
            Order_CartList: this.data.menuID,           //产品数量
            User_ID: app.Data.userInfo.User_ID,
            Order_TotalPrice: this.data.totalPrice
         }
         // console.log('【添加param】',param)
         co.getShareApi('order/Omg/addFood', param, function (res) {
            // console.log("【添加菜单返回信息】===",res)
            if (res.code == 0) {
               app.Data.tableOrder = res.data
               wx.navigateBack({
                  delta: 2
               })
            }
         })
      }
   },
   //获取桌号对应订单信息
   getTableInfo: function (biz_id, tableId) {
      var param = {
         Biz_ID: biz_id,
         Table_ID: tableId
      }
      co.getShareApi('order/Omg/tableOrder', param, function (res) {
         if (res.code == 0) {//此桌有订单时
            var obj = JSON.stringify(res.data)
            wx.navigateTo({
               url: '/02local/07tableOrder/tableOrder?obj=' + obj,
            })
         }
      })
   }
})